<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/9/4
 * Time: 9:36
 */

namespace app\admin\controller;


use app\admin\model\Bet;
use app\admin\model\Odds;
use Qiniu\Auth;
use think\Cache;
use think\Config;
use think\Request;
use think\Db;

class Article extends Admin
{
    
    public function index(){
        $data = Db::name('article_cate')->select();
        return view('cate_show',[
            'data'=>$data,
        ]);
    }
    /**
     * 文章分类
     */
    public function cate_show(){
        $data = Db::name('article_cate')->select();
        return view('',[
            'data'=>$data,
        ]);
    }

    /**
     * 添加分类
     */
    public function add_cate(Request $request){
        $name = $request->post('name');
        $insert = [
            'name'=>$name
        ];
        $result = Db::name('article_cate')->insert($insert);
        if ($result) {
            $this->success_new('添加成功',url('Admin/article/cate_show'));
        }else{
            $this->error_new('添加失败');
        }

    }

    /**
     * 保存分类
     */
    public function do_bao(Request $request){
        $name = $request->post('name');
        $id   = $request->post('id');
        $up = [
            'name'=>$name
        ];
        $result = Db::name('article_cate')
            ->where('id',$id)
            ->update($up);
        if($result){
            json_return(200,'成功');
        }else{
            json_return(200,'失败');
        }
    }

    /**
     * 删除分类
     */
    public function cate_del(Request $request){
        $id = $request->param('id');
        $has_article = Db::name('article')->where('cate',$id)->find();
        if($has_article){
            $this->error_new('该分类下还有文章');
        }
        $result = Db::name('article_cate')
            ->where('id',$id)
            ->delete();
        if($result){
            $this->error_new('删除成功');
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 新闻文章
     */
    public function article_list(Request $request){
        $cate = $request->param('cate');
        $w = [];
        if($cate){
         $w['a.cate'] = $cate;
        }
        $data = Db::name('article')
            ->alias('a')
            ->field('a.*,c.name')
            ->join('dl_article_cate c','a.cate=c.id')
            ->where($w)
            ->order('id desc')
            ->paginate(20);
        if($data) {
            $list = $data->all();
            foreach ($list as $k => $v) {
                $pic = explode(',', $v['picture']);
                $v['image'] = $pic[0];
                $data[$k] = $v;
            }
        }

        return view('article',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 删除文章
     */
    public function article_del(Request $request){
        $id     = $request->param('id');
        $result   = Db::name('article')->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success_new('删除成功',url('Admin/article/article_list'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 显示新闻
     */
    public function article_show(Request $request){

            $id     = $request->param('id');
            $data   = Db::name('article')->where(array('id'=>$id))->find();
            $pic    = explode(',', $data['picture']);
            $cate = Db::name('article_cate')->select();
            return view('article_edit',[
                'data'=>$data,
                'cate'=>$cate,
                'picture'=>$pic

            ]);
    }

    /**
     * 编辑新闻
     */
    public function article_edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $picture = implode(',',$data['picture']);

            $data['picture'] = $picture;

            $result=Db::name('article')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('Admin/article/article_list'));
            }else{
                $this->error_new('修改失败');
            }
        }
    }

    /**
     * 添加新闻显示
     */
    public function article_do_show(){
        $qiniu_config = config('qiniu');
        $accessKey    = $qiniu_config['ACCESSKEY'];
        $secretKey    = $qiniu_config['SECRETKEY'];
        $auth         = new Auth($accessKey, $secretKey);
        $token        = $auth->uploadToken($qiniu_config['bucket']);
        $domain        = $qiniu_config['domain'];
        $cate = Db::name('article_cate')->select();
        return view('article_add',[
            'cate'=>$cate,
            'token'=>$token,
            'domain'=>$domain
        ]);
    }

    /**
     * 添加新闻
     */
    public function article_add(Request $request){
        if($request->isPost()){
            $data    = $request->post();
            $picture = implode(',',$data['picture']);
            $data['picture'] = $picture;
            $result=Db::name('article')->insert($data);
            if ($result) {
                $this->success_new('添加成功',url('Admin/article/article_list'));
            }else{
                $this->error_new('添加失败');
            }
        }
    }

    /**
     * 评论管理
     */
    public function comment(){
        $data = Db::name('comment_a')
            ->alias('c')
            ->field('c.*,m.nickname')
            ->join('dl_member m','c.uid=m.id')
            ->order('c.id desc')
            ->paginate(20);
        return view('comment',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 删除文章
     */
    public function comment_del(Request $request){
        $id     = $request->param('id');
        $result   = Db::name('comment_a')->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success_new('删除成功',url('Admin/article/comment'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 文章推荐
     */
    public function do_recommend(Request $request){
        $id  = $request->post('id');
        $way = $request->post('way');

        if($way==1){
            $result =Db::name('article')->where('id',$id)->update(['is_recommend'=>1]);
        }elseif ($way==2){
            $result =Db::name('article')->where('id',$id)->update(['is_recommend'=>0]);
        }
        if($result){
            json_return(200,'成功');
        }else{
            json_return(500,'失败');
        }
    }

    /**
     * 文章推送
     */
    public function push(Request $request){
        $id = $request->post('id');
        $data = Db::name('article')->field('id,title,picture')->where('id',$id)->find();
        $title = $data['title'];
        $pics = explode(',',$data['picture']);
        $data['picture'] = Config::get('img_url').$pics[0];
        $data = json_encode($data);
       // push_all_article($title,$data);
       json_return('200','推送成功');
    }

}